<template>
  <div>
    <x-dialog
    v-model="showValue"
    :dialogClass="theme === 'android' ? 'weui-dialog weui-skin_android' : 'weui-dialog'"
    :mask-transition="maskTransition"
    :dialog-transition="theme === 'android' ? 'vux-fade' : dialogTransition"
    :hide-on-blur="hideOnBlur"
    :mask-z-index="maskZIndex"
    @on-hide="$emit('on-hide')">
      <div class="weui-dialog__hd" v-if="!!title">
        <strong class="weui-dialog__title">{{ title }}</strong>
      </div>
      <div class="weui-dialog__bd">
        <slot><img class=" shake-vertical-slow shake-constant shake" :src="imgurl"><div v-html="content"></div></slot>
      </div>
      <div class="weui-dialog__ft">
        <a href="javascript:;" class="weui-dialog__btn weui-dialog__btn_default" @click="_onCancel">{{cancelText || $t('cancel_text')}}</a>
        <a href="javascript:;" v-show="!!showDelay" class="weui-dialog__btn weui-dialog__btn_primary" @click="_onDelay">{{delayText || $t('delay_text')}}</a>
      </div>
    </x-dialog>
  </div>
</template>

<i18n>
delay_text:
  en: delay
  zh-CN: 延迟
cancel_text:
  en: cancel
  zh-CN: 关闭
</i18n>

<script>
import XDialog from '../x-dialog'
export default {
  name: 'clock',
  components: {
    XDialog
  },
  props: {
    value: {
      type: Boolean,
      default: false
    },
    showDelay: {
      type: Boolean,
      default: false
    },
    autoClose: { //设置为0时,不会自动关闭
      type: Number,
      default: 10
    },
    placeholder: {
      type: String,
      default: ''
    },
    clockImg: {
      type: String,
      default: ''
    },
    mp3url: {
      type: String,
      default: ''
    },
    clockSnd: {
      type: String,
      default: ''
    },
    theme: {
      type: String,
      default: 'ios'
    },
    hideOnBlur: {
      type: Boolean,
      default: false
    },
    title: String,
    delayText: String,
    cancelText: String,
    maskTransition: {
      type: String,
      default: 'vux-fade'
    },
    maskZIndex: [Number, String],
    dialogTransition: {
      type: String,
      default: 'vux-dialog'
    },
    content: String,
    closeOnDelay: {
      type: Boolean,
      default: true
    },
    inputAttrs: Object
  },
  created () {
    this.showValue = this.show
    if (this.value) {
      this.showValue = this.value
    }
  },
  watch: {
    value (val) {
      this.showValue = val
    },
    showValue (val) {
      this.$emit('input', val)
      if (val) {        
        this.$emit('on-show') // emit just after msg is cleared
		this.imgurl = this.clockImg || '//cdns.7dtime.com/z39/z39h5/images/clockImg.png';
		this.audio = this.audio || new Audio();
		this.audio.src = this.mp3url || '//cdns.7dtime.com/z39/files/clock.mp3'  //音乐的路径
		this.audio.loop=true; 
		this.audio.play();
		if( this.autoClose >0){
		  this.timeoutId = setTimeout(()=>{
			this._onCancel();
			this.timeoutId = -1 ;
			console.log("time out clock");
		  },this.autoClose *1000)
		}
      }else{
		if(this.audio) this.audio.pause();
	  }
    }
  },
  data () {
    return {
      msg: '',
	  imgurl:'',
      showValue: false,
      timeoutId:-1,
	  audio:null,
    }
  },
  methods: {
    setInputValue (val) {
      this.msg = val
    },
    _onDelay () {
      if (!this.showValue) {
        return
      }
      if (this.closeOnDelay) {
        this.showValue = false
      }
      this.$emit('on-delay', this.msg)
      this.clearTimer();
    },
    _onCancel () {
      if (!this.showValue) {
        return
      }
      this.showValue = false
      this.$emit('on-cancel')
      this.clearTimer();
    },
    clearTimer(){
      if(this.timeoutId==-1) return ;
      clearTimeout( this.timeoutId ) ;
      this.timeoutId = -1 ;
    }
  }
}
</script>

<style lang="less">
@import '../../styles/transition.less';
@import '../../styles/weui/widget/weui_tips/weui_mask';
@import '../../styles/weui/widget/weui_tips/weui_dialog';

.vux-prompt {
  padding-bottom: 1.6em;
}

.vux-prompt-msgbox {
  width: 80%;
  border: 1px solid #dedede;
  border-radius: 5px;
  padding: 4px 5px;
  appearance: none;
  outline: none;
  font-size: 16px;
}
</style>
